package com.zhu.shopcenter.controller;


import com.zhu.commons.utils.JwtInfo;
import com.zhu.commons.utils.JwtUtils;
import com.zhu.commons.utils.R;
import com.zhu.service.base.handler.error.LoginErrorException;
import com.zhu.shopcenter.pojo.TradeOrder;
import com.zhu.shopcenter.service.ITradeOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-08-19
 */
@RestController
@RequestMapping("api/trade/order")
public class TradeOrderController {
    @Autowired
    private ITradeOrderService tradeOrderService;

    @ApiOperation(value = "生成订单")
    @PostMapping("auth/generateOrder/{courseId}")
    public R generateOrder(@PathVariable String courseId, HttpServletRequest request) throws LoginErrorException, Exception {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            return R.error().message("请登录！");
        }
        String orderId = tradeOrderService.saveOrder(courseId, jwtInfo.getId());
        return R.ok().data("orderId", orderId);
    }

    @ApiOperation(value = "根据id查询订单")
    @PostMapping("auth/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId, HttpServletRequest request) throws LoginErrorException, Exception {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            return R.error().message("请登录！");
        }

        TradeOrder order = tradeOrderService.getOrderInfo(orderId, jwtInfo.getId());
        return R.ok().data("item", order);
    }

    @ApiOperation(value = "判断课程是否购买")
    @PostMapping("auth/buyStatus/{courseId}")
    public R buyStatus(@PathVariable String courseId, HttpServletRequest request) throws LoginErrorException, Exception {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if (jwtInfo == null) {
            return R.error().message("请登录！");
        }
        Boolean buyStatus = tradeOrderService.buyStatus(courseId, jwtInfo.getId());
        return R.ok().data("item", buyStatus);
    }

    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("/auth/getOrderList")
    public R getOrderList(HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if(jwtInfo==null){
            return R.error().message("请登录！");
        }
        String id = jwtInfo.getId();
        List<TradeOrder> orderList= tradeOrderService.getOrderList(id);
        if(orderList!=null){
            return R.ok().data("orderList",orderList);
        }
        return R.error().message("尚未购买任何订单!");
    }

    @ApiOperation(value = "删除订单(批量)")
    @DeleteMapping("/auth/deleteOrder")
    public R deleteOrder(HttpServletRequest request,@RequestBody List<String> idList){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if(jwtInfo==null){
            return R.error().message("请登录！");
        }
        String id = jwtInfo.getId();

        if(tradeOrderService.deleteOrderList(idList,id)){
            return R.ok().message("订单删除成功！");
        }
        return R.error().message("订单删除失败!");
    }
    @ApiOperation(value = "删除订单(单个)")
    @DeleteMapping("/auth/deleteOrderById/{orderId}")
    public R deleteOrderById(HttpServletRequest request,@PathVariable String orderId){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        if(jwtInfo==null){
            return R.error().message("请登录！");
        }
        String id = jwtInfo.getId();

        if(tradeOrderService.removeById(orderId)){
            return R.ok().message("订单删除成功！");
        }
        return R.error().message("订单删除失败!");
    }
}
